import axios from 'axios'
import {Message} from 'element-ui'
import router from "../router/index"
import {baseUrl} from "./baseUrl"

const service = axios.create({
  baseURL:baseUrl(),// url = base url + request url

  timeout: 500000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {

    if (localStorage.getItem("TOKEN")) {
      config.headers['TOKEN'] = localStorage.getItem("TOKEN")
    }else{

    }

    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(

  response => {
    const res = response
    console.log(res.data.code);
    if (res.data.code ==200||res.data.code==100) {
      return res;

    }else if(res.data.code==800){
        Message.warning("您的权限已失效!请重新登录")
        router.push("/main/login")
        return res
    }
  },
  error => {
      // let msg=[]
      // for(let item of error.data.errors){
      //     msg.push(item.defaultMessage);
      // }
      console.log('err' + error) // for debug
      // Message.warning(msg);
  }
)

export default service
